package org.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.zhxy.model.entity.Admin;
import org.zhxy.service.AdminService;
import org.zhxy.mapper.AdminMapper;
import org.springframework.stereotype.Service;

/**
 * @author walter
 * @description 针对表【tb_admin】的数据库操作Service实现
 * @createDate 2025-07-01 10:22:04
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public Admin findByName(String name) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getName, name);
        return getOne(wrapper);
    }

    @Override
    public IPage<Admin> page(IPage<Admin> page, String adminName) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(adminName), Admin::getName, adminName);
        return page(page, wrapper);
    }

    @Override
    public boolean updatePwd(Integer id, String newPwd) {
        LambdaUpdateWrapper<Admin> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Admin::getId, id);
        wrapper.set(Admin::getPassword, DigestUtils.md5DigestAsHex(newPwd.getBytes()));
        return update(wrapper);
    }
}




